package com.sailing.xjpb.orm.intercepter.impl;

import com.sailing.xjpb.common.SailingConstants;
import com.sailing.xjpb.common.SailingReflectionUtils;
import com.sailing.xjpb.common.operator.*;
import com.sailing.xjpb.common.sercurity.SailingUserContext;
import com.sailing.xjpb.common.sercurity.SailingUserCurrentInfo;
import com.sailing.xjpb.orm.intercepter.CrudRepositoryOperatorHandler;
import com.sailing.xjpb.orm.intercepter.annotation.DbCreateTime;
import com.sailing.xjpb.orm.intercepter.annotation.DbUpdateTime;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * 设置默认的操作人处理类
 */
@SuppressWarnings("deprecation")
public class DefaultCurdRepositoryOperatorHandler implements CrudRepositoryOperatorHandler {
    @Override
    public void setOperator(Object entity, boolean isNew, SailingUserContext userContext) {
        Date operationTime = new Date();
        if (entity instanceof CreateOperator && (isNew || ((CreateOperator) entity).getCreateUserId() == null)) {
            SailingUserCurrentInfo currentUser = userContext.getCurrentUser();
            // 设置新增人信息
            CreateOperator createOperator = (CreateOperator) entity;
            createOperator.setCreateUserId(currentUser.getUserid());
            createOperator.setCreateUserName(currentUser.getUsercname());
            createOperator.setCreateIp(userContext.getClientIpAddress());
            createOperator.setCreateOrgCode(currentUser.getOrgCode());
            createOperator.setCreateOrgName(currentUser.getOrgName());
            createOperator.setCreateTime(operationTime);
        } else if (entity instanceof CreateOperatorV1 && (isNew || ((CreateOperatorV1) entity).getCjrDm() == null)) {
            SailingUserCurrentInfo currentUser = userContext.getCurrentUser();
            // 设置新增人信息
            CreateOperatorV1 createOperator = (CreateOperatorV1) entity;
            createOperator.setCjrDm(currentUser.getUserid());
            createOperator.setCjrXm(currentUser.getUsercname());
            createOperator.setCjdwDwdm(currentUser.getOrgCode());
            createOperator.setCjdwDwmc(currentUser.getOrgName());
            createOperator.setCjsj(operationTime);
        }
        if (entity instanceof EditOperator) {
            SailingUserCurrentInfo currentUser = userContext.getCurrentUser();
            // 设置编辑人信息
            EditOperator editOperator = (EditOperator) entity;
            editOperator.setEditUserId(currentUser.getUserid());
            editOperator.setEditUserName(currentUser.getUsercname());
            editOperator.setEditIp(userContext.getClientIpAddress());
            editOperator.setEditOrgCode(currentUser.getOrgCode());
            editOperator.setEditOrgName(currentUser.getOrgName());
            editOperator.setEditTime(operationTime);
        } else if (entity instanceof EditOperatorV1) {
            SailingUserCurrentInfo currentUser = userContext.getCurrentUser();
            // 设置编辑人信息
            EditOperatorV1 editOperator = (EditOperatorV1) entity;
            editOperator.setXgrDm(currentUser.getUserid());
            editOperator.setXgrXm(currentUser.getUsercname());
            editOperator.setXgdwDwdm(currentUser.getOrgCode());
            editOperator.setXgdwDwmc(currentUser.getOrgName());
            editOperator.setXgsj(operationTime);
        }
        if (entity instanceof CancelOperator && (isNew || ((CancelOperator) entity).getCancelState() == null)) {
            // 设置注销状态
            CancelOperator cancelOperator = (CancelOperator) entity;
            cancelOperator.setCancelState(SailingConstants.BOOLEAN_FALSE);
        }
        if (entity instanceof SailingTenant && (isNew || ((SailingTenant) entity).getTenantCode() == null)) {
            //设置TenantCode
            SailingTenant tenantCode = (SailingTenant) entity;
            tenantCode.setTenantCode(userContext.getTenantCode());
        }
    }

    @Override
    public void setDbTime(Object entity, boolean newEntity, Date dbTime) {
        // 获取实体对象的类
        Class<?> entityClazz = entity.getClass();
        // 获取实体类中所有带有@LastModifiedBy注解的字段
        List<Field> fields = SailingReflectionUtils.getDeclaredFields(entityClazz,
                f -> f.isAnnotationPresent(DbCreateTime.class) || f.isAnnotationPresent(DbUpdateTime.class));
        // 遍历所有符合条件的字段
        for (Field field : fields) {
            // 冗余检查，确保字段带有@LastModifiedBy注解
            if (newEntity && field.isAnnotationPresent(DbCreateTime.class)) {
                // 通过反射将指定的数据库时间设置到字段中
                SailingReflectionUtils.setField(field, entity, dbTime);
            }
            if (field.isAnnotationPresent(DbUpdateTime.class)) {
                // 通过反射将指定的数据库时间设置到字段中
                SailingReflectionUtils.setField(field, entity, dbTime);
            }
        }
    }
}
